package com.adwin.addcoin.ui.main

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.WindowManager
import android.widget.RadioGroup
import android.widget.RadioGroup.OnCheckedChangeListener
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.adwin.addcoin.AddCoinApplication
import com.adwin.addcoin.R
import com.adwin.addcoin.base.BaseActivity
import com.adwin.addcoin.databinding.MainActivityBinding
import com.adwin.addcoin.ui.adapter.MainFragmentManagerAdapter
import com.adwin.addcoin.ui.dialog.CoinsRewardDialog
import com.adwin.addcoin.utils.FlowKeyUtils
import com.adwin.addcoin.utils.TaskStatusNet
import com.cashbird.app.infra.utils.FlowBus
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong


class MainActivity : BaseActivity<MainActivityBinding, MainViewModel>(
    MainViewModel::class.java
),OnCheckedChangeListener {
    private var homeFragments = ArrayList<Fragment>()
    private var extTime = AtomicLong(0)
    private var adapter = MainFragmentManagerAdapter(this,homeFragments)
    override fun inflater(): MainActivityBinding {
        return DataBindingUtil.setContentView(this, R.layout.main_activity)
    }

    override fun initActivity() {
        fullScreen(this)
        FlowBus.with<Long>(FlowKeyUtils.taskStageChange).register(lifecycle){coins ->
            viewModel?.queryUserCoinDetail()
            if (AddCoinApplication.instance.activityDd!= null){
                CoinsRewardDialog(AddCoinApplication.instance.activityDd!!,coins).show()
            }
            lifecycleScope.launch {
                FlowBus.with<String>(FlowKeyUtils.taskChange).post("")
            }

        }
        binding.rbHostHome.isChecked = true
        homeFragments.add(HomeFragment())
        homeFragments.add(TaskFragment())
        homeFragments.add(GameFragment())
        homeFragments.add(PersonalCenterFragment())
        binding.mainPager.adapter = adapter
        binding.mainPager.isUserInputEnabled = false
        binding.mainPager.offscreenPageLimit = homeFragments.size
        binding.mainPager.registerOnPageChangeCallback(callback)
        binding.rgMain.setOnCheckedChangeListener(this)


        if (AddCoinApplication.isCloak){
            binding.rbHostGames.visibility = View.GONE
        }
        FlowBus.with<String>(FlowKeyUtils.CoinsChange).register(lifecycle) {
            getInviteDetails()
        }
        getInviteDetails()
        FlowBus.with<String>(FlowKeyUtils.inviteChange).register(lifecycle) {
            getInviteDetails()
        }
    }

    private fun getInviteDetails(){
        viewModel?.getAppDisposition()
        viewModel?.getInviteDetails()
        viewModel?.queryUserCoinDetail()
    }

    private val callback = object :OnPageChangeCallback(){

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            when(position){
                0 -> binding.rbHostHome.isChecked = true
                1 -> binding.rbHostMissions.isChecked = true
                2 -> binding.rbHostGames.isChecked = true
                3 -> binding.rbHostCenter.isChecked = true
            }
        }
    }

    override fun onStart() {
        super.onStart()
        TaskStatusNet.startStepStatus()
    }

    /**
     * 当前用户点击tab回调
     */
    override fun onCheckedChanged(p0: RadioGroup?, p1: Int) {
        TaskStatusNet.startStepStatus()
        when(p1){
            binding.rbHostHome.id -> {

                binding.mainPager.setCurrentItem(0,false)
            }
            binding.rbHostCenter.id -> {
                binding.mainPager.setCurrentItem(3,false)
            }
            binding.rbHostGames.id -> {
                binding.mainPager.setCurrentItem(2,false)
            }
            binding.rbHostMissions.id -> {
                binding.mainPager.setCurrentItem(1,false)
            }
        }
    }




    override fun onBackPressed() {
        val time = System.currentTimeMillis()
        if (binding.rbHostGames.isChecked){
            if ((homeFragments[2] as GameFragment).onBackPressed()){
                if(time - extTime.get() < 3000){
                    super.onBackPressed()
                }else{
                    extTime.set(time)
                    Toast.makeText(this,"Click back again to exit",Toast.LENGTH_LONG).show()
                }
            }
        }else if (time - extTime.get() < 3000){
            super.onBackPressed()
        }else{
            extTime.set(time)
            Toast.makeText(this,"Click back again to exit",Toast.LENGTH_LONG).show()
        }
    }
}